package com.ruoyi.flowable.listener.scrap;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.equ.domain.EquScrap;
import com.ruoyi.flowable.service.IWorkOrderAssetRelService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * 报废-2-1监听：使用科室
 */
@Slf4j
@Component("scrapListener_2_1")
public class ScrapListener_2_1 implements ExecutionListener {
    private static final ObjectMapper objectMapper = new ObjectMapper();
    private static final IWorkOrderAssetRelService workOrderAssetRelService = SpringUtils.getBean(IWorkOrderAssetRelService.class);

    @Override
    public void notify(DelegateExecution execution) {
        try {
            //使用科室
            String flag2 = execution.getVariable("flag2", String.class);
            String proposal2 = execution.getVariable("proposal2", String.class);
            HashMap<String, Object> useMap = new HashMap<>();
            useMap.put("确认设备现状与报废原因属实", flag2);
            useMap.put("科室内部评估意见", proposal2);

            HashMap<String, Object> approvalSummaryMap = new HashMap<>();
            approvalSummaryMap.put("使用科室", useMap);
            String approvalSummary = objectMapper.writeValueAsString(approvalSummaryMap);

            EquScrap equScrap = execution.getVariable("equScrap", EquScrap.class);

            equScrap.setApprovalSummary(approvalSummary);

            execution.setVariable("equScrap", equScrap);

            //更新状态
            String processInstanceId = execution.getProcessInstanceId();
            workOrderAssetRelService.updateWorkOrderStatus(processInstanceId, "处理中", null);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
